package com.web.portal.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.portal.pojo.Category;
import com.web.portal.response.ResponseResult;
import com.web.portal.services.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 管理中心，分类Api
 */
@Api(tags = "分类管理")
@Slf4j
@RestController
@RequestMapping("/admin/category")
public class CategoryAdminApi {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 添加分类
     * 需要管理员权限
     *
     * @param category
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加分类")
    public ResponseResult addCategory(@RequestBody Category category) {
        boolean flag = categoryService.save(category);
        if (flag) {
            return ResponseResult.SUCCESS("添加成功");
        } else {
            return ResponseResult.FAILED("添加失败");
        }
    }

    /**
     * 获取分类
     *
     * @param categoryId
     * @return
     */
    @ApiOperation(value = "用id获取分类")
    @GetMapping("find_categories/{categoryId}")
    public ResponseResult getCategory(@PathVariable("categoryId") String categoryId) {
        Category category = categoryService.getById(categoryId);
        if (category == null) {
            return ResponseResult.FAILED("查找失败");
        }
        category.setState(getState(category.getState()));
        return ResponseResult.SUCCESS().setData(category);
    }

    /**
     * 获取分类列表
     * <p>
     * 权限：管理员权限
     *
     * @return
     */
    @ApiOperation(value = "获取分类列表")
    @GetMapping("/list")
    public ResponseResult listCategories() {
        return ResponseResult.SUCCESS("获取分类列表成功.")
                .setData(categoryService.list());
    }

    //3 条件查询带分页
    @ApiOperation(value = "分类查询带分页")
    @PostMapping("find_categories/{current}/{limit}")
    public ResponseResult findCategories(@PathVariable long current,
                                         @PathVariable long limit) {
        //创建page对象，传递当前页，每页记录数
        Page<Category> page = new Page<>(current, limit);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        //调用方法实现分页查询
        Page<Category> pageHospitalSet = categoryService.page(page, wrapper);
        //返回结果
        return ResponseResult.SUCCESS("获取分类列表成功.")
                .setData(pageHospitalSet);
    }

    /**
     * 更新分类
     *
     * @param category
     * @return
     */
    @PostMapping("/updateCategory")
    @ApiOperation(value = "更新分类")
    public ResponseResult updateCategory(@RequestBody Category category) {
        boolean flag = categoryService.updateById(category);
        if (flag) {
            return ResponseResult.SUCCESS("更改成功");
        } else {
            return ResponseResult.FAILED("更改失败");
        }
    }

    @DeleteMapping("/{categoryId}")
    @ApiOperation(value = "删除分类")
    public ResponseResult deleteCategory(@PathVariable("categoryId") String categoryId) {
        boolean flag = categoryService.removeById(categoryId);
        if (flag) {
            return ResponseResult.SUCCESS("删除成功");
        } else {
            return ResponseResult.FAILED("删除失败");
        }
    }

    public String getState(String state) {
        switch (state) {
            case "0":
                return "删除";
            case "1":
                return "正常";
            default:
                return state;
        }
    }
}
